@extends('common.base-master')

@section('html-title')
    <title>波罗鸡-添加或修改地址</title>
@endsection

@section('html-head')
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/city-picker.min.js"></script>
@endsection

@section('html-body')
    <div class="changeadd-page">
        <div class="changeadd-form">
            <form action="post" class="">
                <div class="input-container">
                    <span class="input-title">收件人</span>
                    <input type="text" name="nickname" id="realname" placeholder="姓名" value="@if($address){{$address->realname}}@endif">
                </div>
                <div class="input-container">
                    <span class="input-title">手机号码</span>
                    <input type="text" name="nickname" id="phone"  placeholder="11位手机号" value="@if($address){{$address->phone}}@endif">
                </div>
                <div class="input-container">
                    <span class="input-title">地区信息</span>
                    <input type="text" name="nickname" id="city-picker" placeholder="地区信息" value="@if($address){{$address->province}} {{$address->city}} {{$address->area}}@endif">
                </div>
                <div class="input-container">
                    <span class="input-title">详细地址</span>
                    <input type="text" name="nickname" id="info"  placeholder="街道门牌信息" value="@if($address){{$address->info}}@endif">
                </div>
                <div class="input-container" style="border: none;">
                    <span class="input-title">默认地址</span>
                    <div class="defult-radio">
                        <label for="not">
                            <input type="radio" value="0" name="defultadd" id="not" @if(!$address || ($address && $address->is_default == 0))checked @endif />
                            否
                        </label>
                        <label for="yes" style="margin-left: 10px">
                            <input type="radio" value="1" name="defultadd" id="yes" @if($address && $address->is_default == 1)checked @endif />
                            是
                        </label>
                    </div>
                </div>
                <input type="hidden" id="addr_id" value="{{$id}}" />
                <div class="submit-btn" id="btn_save" style="text-align: center;margin-top: 50px">保存</div>
            </form>
        </div>
    </div>
    <script>
        $(document).ready(function () {
            $("#city-picker").cityPicker({
                title: "地区信息"
            });

            $('#btn_save').click(function () {
                var realname  = $('#realname').val();
                var phone     = $('#phone').val();
                var district  = $('#city-picker').val();
                var info      = $('#info').val();
                var isDefault = $('input:radio[name="defultadd"]:checked').val();
                var id        = $('#addr_id').val();

                if(realname == ''){
                    $.toast('请输入收货人',"forbidden");
                    $('#realname').focus();
                    return false;
                }

                if(phone == ''){
                    $.toast('请输入联系方式',"forbidden");
                    $('#phone').focus();
                    return false;
                }

                if(district == ''){
                    $.toast('请选择地址',"forbidden");
                    return false;
                }

                if(info == ''){
                    $.toast('详细地址不能为空',"forbidden");
                    $('#info').focus();
                    return false;
                }

                $.ajax({
                    url:"{{route('Api.User.setUserAddress')}}",
                    type:'post',
                    dataType:'json',
                    data:{
                        realname:realname,
                        phone:phone,
                        district:district,
                        info:info,
                        id:id,
                        is_default:isDefault
                    },
                    success:function (obj) {
                        if(obj.level == 'success'){
                            $.toast(obj.message,function() {
                                window.location.href="{{route('Home.User.myNewAddress')}}";
                            });
                        }else {
                            $.toast(obj.message,"forbidden");
                        }
                    },
                    error:function (obj) {
                        console.log(obj);
                    }
                });
            });
        })
    </script>
@endsection